package com.liuda066.forumssm.services.impl;

import com.liuda066.forumssm.common.AppResult;
import com.liuda066.forumssm.common.ResultCode;
import com.liuda066.forumssm.dao.ArticleMapper;
import com.liuda066.forumssm.dao.ArticleReplyMapper;
import com.liuda066.forumssm.exception.ApplicationException;
import com.liuda066.forumssm.model.Article;
import com.liuda066.forumssm.model.ArticleReply;
import com.liuda066.forumssm.services.IArticleReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/9/5 21:54
 */
@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public void create(ArticleReply articleReply) {
        if (articleReply == null || articleReply.getArticleId()==null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Article article = articleMapper.selectByPrimaryKey(articleReply.getArticleId());
        if (article == null){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString()+". articleId = "+articleReply.getArticleId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //状态判定
        if (article.getState() != 0 || article.getDeleteState() != 0){
            log.info("帖子状态异常. articleId = "+articleReply.getArticleId()+
                    ". state = "+article.getState()+". deleteState = "+article.getDeleteState());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //封装
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        articleReply.setLikeCount(0);
        Date date = new Date();
        articleReply.setUpdateTime(date);
        articleReply.setCreateTime(date);

        int row = articleReplyMapper.insertSelective(articleReply);
        if (row!=1){
            log.info("新增帖子回复失败. userId = "+articleReply.getPostUserId()+
                    ". articleId = "+articleReply.getArticleId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //更新原帖子回复数
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount()+1);
        articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (row!=1){
            log.info("原帖子回复数更新失败. userId = "+articleReply.getPostUserId()+
                    ". articleId = "+articleReply.getArticleId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        log.info("新增帖子回复成功,userId = "+articleReply.getPostUserId() +
                ",articleId = "+articleReply.getArticleId() + ", articleReplyId  = "+articleReply.getId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        if (articleId == null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        return articleReplyMapper.selectByArticleId(articleId);
    }
}
